//
//  ByViewServiceForInHospitalSegment.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/12/11.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewServiceForInHospitalSegment.h"

@implementation ByViewServiceForInHospitalSegment
{
    UIButton *_prehandMoneyButton;  // 预交付金额
    UIButton *_dailyBillButton;     // 日清单
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    UIColor *color = rgba(148, 214, 250, 1);
    
    void (^buttonConfig)(UIButton *button, NSString *text) = ^ (UIButton *button, NSString *text) {
        button.titleLabel.font = FONT(16);
        button.layer.masksToBounds = true;
        button.layer.cornerRadius = 8;
        button.layer.borderColor = color.CGColor;
        button.layer.borderWidth = 1;
        [button setTitle:text forState:UIControlStateNormal];
        [button setTitleColor:color forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:color] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
    };
    
    _prehandMoneyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _prehandMoneyButton.tag = 0;
    buttonConfig(_prehandMoneyButton, @"预交付金额");
    [self addSubview:_prehandMoneyButton];
    
    _dailyBillButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _dailyBillButton.tag = 1;
    buttonConfig(_dailyBillButton, @"日清单");
    [self addSubview:_dailyBillButton];
    
    
    [_prehandMoneyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.height.equalTo(@40);
        make.centerY.equalTo(@0);
        make.right.equalTo(self.mas_centerX).offset(-10);
    }];
    [_dailyBillButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.centerY.equalTo(_prehandMoneyButton);
        make.left.equalTo(self.mas_centerX).offset(10);
    }];
    
    // init
    [self segmentAction:_prehandMoneyButton];
}

- (void)segmentAction:(UIButton *)button {
    
    if (button == _prehandMoneyButton) {
        _prehandMoneyButton.selected = true;
        _dailyBillButton.selected = false;
    } else {
        _prehandMoneyButton.selected = false;
        _dailyBillButton.selected = true;
    }
    
    if (self.segmentSelectIndexBlock) {
        self.segmentSelectIndexBlock(button.tag);
    }
}

@end
